﻿using Assets.Scripts.DL.Consts;
using Assets.Scripts.DL.Enemy.CommonEnemy;
using Assets.Scripts.DL.Mgrs;
using UnityEngine;

public class BaseAtkDetect {
	readonly Transform _entityTransform;
	//bool _canAtk = true;
	LayerMask _layers;

	public BaseAtkDetect(Transform trans, LayerMask layers) {
		_entityTransform = trans;
		_layers = layers;
	}

	public bool CheckAtk(Vector2 offset, float range, int damge) {
		var pos = new Vector2(_entityTransform.position.x, _entityTransform.position.y);
		var dir = (int)_entityTransform.localScale.x;
		var realPos = new Vector2(pos.x + dir * offset.x, pos.y + offset.y);
		Collider2D[] hitTargets = Physics2D.OverlapCircleAll(realPos, range, _layers);
		//Debug.Log($"{hitTargets.Length}");
		foreach (Collider2D target in hitTargets) {
			target.GetComponent<BaseController>()?.TakeDamage(damge);
			//玩家有俩碰撞体

			if (target.gameObject.CompareTag("Player")) {
				break;
			}
		}
		return hitTargets.Length != 0; //判断有没有击打到物体
	}
}